package com.nutiteq.directions;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

import com.nutiteq.components.WgsPoint;
import com.nutiteq.services.OpenLSDirections;

public class RoutingServiceList extends List implements CommandListener {
  private final DirectionsCanvas directionsCanvas;
  private final Command back;
  private final Command ok;
  private final WgsPoint startCoordinates;
  private final WgsPoint endCoordinates;

  public RoutingServiceList(final DirectionsCanvas directionsCanvas,
      final WgsPoint startCoordinates, final WgsPoint endCoordinates) {
    super("Routing service", List.IMPLICIT);
    this.directionsCanvas = directionsCanvas;
    this.startCoordinates = startCoordinates;
    this.endCoordinates = endCoordinates;
    append("YourNavigation routing", null);
    append("CloudMade routing", null);
//    append("OpenLS routing (Estonia only)", null); // does not work anymore!

    back = new Command("Back", Command.BACK, 0);
    ok = new Command("Ok", Command.OK, 0);

    addCommand(back);
    addCommand(ok);

    setCommandListener(this);
  }

  public void commandAction(final Command cmd, final Displayable d) {
    if (cmd == back) {
      directionsCanvas.show(directionsCanvas);
    } else if (cmd == ok) {
      final int selected = getSelectedIndex();
      if (selected == 0) {
        directionsCanvas.show(new YourNavigationOptionsForm(directionsCanvas, startCoordinates,
            endCoordinates));
      } else if (selected == 1) {
        directionsCanvas.show(new CloudMadeOptionsForm(directionsCanvas, startCoordinates,
            endCoordinates));
      } else if (selected == 2) {
        directionsCanvas.setDirectionsService(new OpenLSDirections(directionsCanvas,
            OpenLSDirections.NUTITEQ_DEFAULT_SERVICE_URL, "et", startCoordinates, endCoordinates));
      }
    }
  }
}
